﻿namespace _37_C_中checked和unchecked
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "C#中checked和unchecked关键字";
            try
            {
                //unchecked
                //{
                //    int a = 80000;
                //    short b = (short)a;
                //    Console.WriteLine(b);
                //}

                //int a = 80000;
                //short b = unchecked((short)a);
                //Console.WriteLine(b);

                //int a = 200;
                //int b = 200;
                //byte c = checked((byte)(a+b));
                //Console.WriteLine(c);

                checked { Calc(); }//对方法无效 只能对代码块有用
            }
            catch (OverflowException ex)
            {

                Console.WriteLine(ex.Message);
            }

            Console.ReadKey();
        }
        public static void Calc()
        {
            int a = 200;
            int b = 200;
            byte c = (byte)(a + b);
            Console.WriteLine(c);
        }
    }
}
